<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 2020-06-17
 * Time: 16:21
 * 创建项目孵化评价
 */

namespace app\api\service\project;


use app\api\model\Member;
use app\api\model\ProjectHatch;
use app\api\model\ProjectHatchComment;
use app\api\model\ProjectHatchLook;
use app\api\model\ProjectHatchLookOrder;
use app\api\model\System;
use app\api\service\Token;
use app\api\validate\project\CheckHatchComment;
use app\lib\exception\ParameterException;
use think\Db;
use think\Exception;

class CommentSer extends Token
{
    /**
     * 1.判断用户权限
     * 2.判断数据
     * 3.判断是否有评论的权限
     * 3.没有直接提示
     * 4.有的话，创建信息并修改 订单的使用状态
     * 5.返回创建成功
     */
    // 用户ID
    private $uid;

    // 会员信息
    private $member;

    // 添加信息
    private $postArr;

    // 订单信息
    private $order;

    function __construct($post)
    {
        $this->uid = self::getCurrentUid();
        self::judgeMember();
        (new CheckHatchComment())->goCheck();
        $this->postArr = $post;
    }

    // 入口文件
    public function entrance()
    {
        $this->member = Member::where('user_id',$this->uid)->find();
        return $this->judgeHatchCommentOrder();
    }

    // 判断项目的权限
    private function judgeHatchCommentOrder()
    {

        $info = ProjectHatch::get($this->postArr['project_hatch_id']);
        if(!$info) {
            throw new ParameterException([
                'msg' => [
                    'hatch' => '请选择正确的项目'
                ]
            ]);
        } else {
            // 判断是否是自己发布的项目
            if($info['user_id'] == $this->uid) {
                return $this->createData();
            } else {
                $time = time();
                $where[] = ['user_id','eq',$this->uid];
                $where[] = ['start_time','elt',$time];
                $where[] = ['end_time','egt',$time];
                $where[] = ['project_hatch_id','egt',$this->postArr['project_hatch_id']];
                $info = ProjectHatchLook::where($where)->find();
                if(!$info) {
                    $order = ProjectHatchLookOrder::where('status',2)
                        ->where('type',1)
                        ->where('user_id',$this->uid)
                        ->find();
                    if($order) {
                        $system = System::get(1);
                        $end_time = $time + ($system['project_hatch_look_day']*24*3600);
                        $mode = ProjectHatchLook::create([
                            'user_id' => $this->uid,
                            'start_time' => $time,
                            'end_time' => $end_time,
                            'project_hatch_id' => $this->postArr['project_hatch_id'],
                        ]);
                        $ProjectHatchLookOrder = new ProjectHatchLookOrder();
                        $ProjectHatchLookOrder->save([ 'project_hatch_look_id' => $mode->id, 'type'=> 2],[ 'id' => $order['id'] ]);
                    } else {
                        $projectHatch = ProjectHatch::get($this->postArr['project_hatch_id']);
                        if($projectHatch['user_id'] != $this->uid) {
                            throw new ParameterException([
                                'errorCode' => 20007,
                                'msg' => [
                                    'hatch' => '您没有项目查看的权限'
                                ]
                            ]);
                        }
                    }
                }
                $this->createData();
            }
        }
    }

    // 创建数据
    private function createData()
    {
        Db::startTrans();
        try{
            $this->postArr['name'] = $this->member['name'];
            $this->postArr['user_id'] = $this->uid;
            ProjectHatchComment::create($this->postArr);
            Db::commit();
            return writeJson(201, [], '添加成功！');
        }
        catch (Exception $ex) {
            //回滚事务
            Db::rollback();
            throw $ex;
        }
    }
}